package com.yehebl.handler.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class DataConvert {
	private static final Map<String, IDataConvert> baseTypeMap = new HashMap<String, IDataConvert>();
	private static final Map<Class<?>, IDataConvert> classMap = new HashMap<Class<?>, IDataConvert>();
	static {
		classMap.put(List.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String string = json.getString(key);
				if (string != null) {
					map.put(key, JSONObject.parseArray(string, clazz[0]));
					return;
				}
				map.put(key, null);
			}
		});
		classMap.put(Map.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				JSONObject jsonObject = json.getJSONObject(key);
				if (jsonObject != null) {
					Map<Object, Object> result = new HashMap<Object, Object>();
					result.putAll(jsonObject);
					map.put(key, result);
					return;
				}
				map.put(key, null);
			}
		});
		baseTypeMap.put("other", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String object = json.getString(key);
				if (object == null) {
					object = json.toJSONString();
				}
				if(object==null){
					map.put(key, null);
				}else{
					map.put(key, JSONObject.parseObject(object,clazz[0]));
				}
			}
		});

		classMap.put(String.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getString(key));
			}
		});

		classMap.put(String[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<String> list = JSONObject.parseArray(jsonStr, String.class);
				if (list == null) {
					return;
				}
				String[] result = new String[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Integer.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getInteger(key));
			}
		});

		classMap.put(Integer[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Integer> list = JSONObject.parseArray(jsonStr, Integer.class);
				if (list == null) {
					return;
				}
				Integer[] result = new Integer[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Double.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getDouble(key));
			}
		});

		classMap.put(Double[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Double> list = JSONObject.parseArray(jsonStr, Double.class);
				if (list == null) {
					return;
				}
				Double[] result = new Double[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Short.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getShort(key));
			}
		});
		classMap.put(Short[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Short> list = JSONObject.parseArray(jsonStr, Short.class);
				if (list == null) {
					return;
				}
				Short[] result = new Short[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Float.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getFloat(key));
			}
		});

		classMap.put(Float[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Float> list = JSONObject.parseArray(jsonStr, Float.class);
				if (list == null) {
					return;
				}
				Float[] result = new Float[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Boolean.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getBoolean(key));
			}
		});

		classMap.put(Boolean[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Boolean> list = JSONObject.parseArray(jsonStr, Boolean.class);
				if (list == null) {
					return;
				}
				Boolean[] result = new Boolean[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Date.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getDate(key));
			}
		});

		classMap.put(Date[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Date> list = JSONObject.parseArray(jsonStr, Date.class);
				if (list == null) {
					return;
				}
				Date[] result = new Date[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Byte.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getByte(key));
			}
		});

		classMap.put(Byte[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getBytes(key));
			}
		});

		classMap.put(BigInteger.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getBigInteger(key));
			}
		});

		classMap.put(BigInteger[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<BigInteger> list = JSONObject.parseArray(jsonStr, BigInteger.class);
				if (list == null) {
					return;
				}
				BigInteger[] result = new BigInteger[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(BigDecimal.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getBigDecimal(key));
			}
		});

		classMap.put(BigDecimal[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<BigDecimal> list = JSONObject.parseArray(jsonStr, BigDecimal.class);
				if (list == null) {
					return;
				}
				BigDecimal[] result = new BigDecimal[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Long.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getLong(key));
			}
		});

		classMap.put(Long[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Long> list = JSONObject.parseArray(jsonStr, Long.class);
				if (list == null) {
					return;
				}
				Long[] result = new Long[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});
		classMap.put(Timestamp.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getTimestamp(key));
			}
		});

		classMap.put(Timestamp[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Timestamp> list = JSONObject.parseArray(jsonStr, Timestamp.class);
				if (list == null) {
					return;
				}
				Timestamp[] result = new Timestamp[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i);
				}
				map.put(key, result);
			}
		});

		classMap.put(Character.class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String string = json.getString(key);
				if (string == null || string.length() < 1) {
					map.put(key, null);
				} else {
					map.put(key, string.charAt(0));
				}

			}
		});

		classMap.put(Character[].class, new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<String> list = JSONObject.parseArray(jsonStr, String.class);
				if (list == null) {
					return;
				}
				Character[] result = new Character[list.size()];
				for (int i = 0; i < result.length; i++) {
					String string = list.get(i);
					;
					if (string == null || string.length() < 1) {
						result[i] = null;
					} else {
						result[i] = string.charAt(0);
					}
				}
				map.put(key, result);
			}
		});

		baseTypeMap.put(Integer.TYPE.getName(), new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getIntValue(key));
			}
		});

		baseTypeMap.put("[I", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Integer> list = JSONObject.parseArray(jsonStr, Integer.class);
				if (list == null) {
					return;
				}
				int[] result = new int[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i).intValue();
				}
				map.put(key, result);
			}
		});

		baseTypeMap.put(Double.TYPE.getName(), new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getDoubleValue(key));
			}
		});

		baseTypeMap.put("[D", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Double> list = JSONObject.parseArray(jsonStr, Double.class);
				if (list == null) {
					return;
				}
				double[] result = new double[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i).doubleValue();
				}
				map.put(key, result);
			}
		});

		baseTypeMap.put(Short.TYPE.getName(), new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getShortValue(key));
			}
		});
		baseTypeMap.put("[S", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Short> list = JSONObject.parseArray(jsonStr, Short.class);
				if (list == null) {
					return;
				}
				short[] result = new short[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i).shortValue();
				}
				map.put(key, result);
			}
		});

		baseTypeMap.put(Float.TYPE.getName(), new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getFloatValue(key));
			}
		});

		baseTypeMap.put("[F", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Float> list = JSONObject.parseArray(jsonStr, Float.class);
				if (list == null) {
					return;
				}
				float[] result = new float[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i).floatValue();
				}
				map.put(key, result);
			}
		});

		baseTypeMap.put(Boolean.TYPE.getName(), new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getBooleanValue(key));
			}
		});

		baseTypeMap.put("[Z", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Boolean> list = JSONObject.parseArray(jsonStr, Boolean.class);
				if (list == null) {
					return;
				}
				boolean[] result = new boolean[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i).booleanValue();
				}
				map.put(key, result);
			}
		});

		baseTypeMap.put(Byte.TYPE.getName(), new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getByteValue(key));
			}
		});

		baseTypeMap.put("[B", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getBytes(key));
			}
		});

		baseTypeMap.put(Long.TYPE.getName(), new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				map.put(key, json.getLongValue(key));
			}
		});

		baseTypeMap.put("[J", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<Long> list = JSONObject.parseArray(jsonStr, Long.class);
				if (list == null) {
					return;
				}
				long[] result = new long[list.size()];
				for (int i = 0; i < result.length; i++) {
					result[i] = list.get(i).longValue();
				}
				map.put(key, result);
			}
		});

		baseTypeMap.put(Character.TYPE.getName(), new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String string = json.getString(key);
				if (string == null || string.length() < 1) {
					map.put(key, '\u0000');
				} else {
					map.put(key, string.charAt(0));
				}

			}
		});

		baseTypeMap.put("[C", new IDataConvert() {
			@Override
			public void convert(Map<Object, Object> map, JSONObject json, String key, Class<?> ... clazz) {
				String jsonStr = json.getString(key);
				if (jsonStr == null) {
					return;
				}
				List<String> list = JSONObject.parseArray(jsonStr, String.class);
				if (list == null) {
					return;
				}
				char[] result = new char[list.size()];
				for (int i = 0; i < result.length; i++) {
					String string = list.get(i);
					if (string == null || string.length() < 1) {
						result[i] = '\u0000';
					} else {
						result[i] = string.charAt(0);
					}
				}
				map.put(key, result);
			}
		});

	}

	public static void addConvert(Class<?> clazz, IDataConvert dc) {
		classMap.put(clazz, dc);
	}
	public static void addConvert(String name, IDataConvert dc) {
		baseTypeMap.put(name, dc);
	}

	public static IDataConvert get(String name) {
		return baseTypeMap.get(name);
	}

	public static IDataConvert get(Class<?> clazz) {
		return classMap.get(clazz);
	}
}
